﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace $ext_safeprojectname$
{
    /// <summary>
    /// 
    /// </summary>
    public class $ext_safeprojectname$DbContextFactory : IDesignTimeDbContextFactory<$ext_safeprojectname$DbContext>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public $ext_safeprojectname$DbContext CreateDbContext(string[] args)
        {
            //这个仅仅在EF的时候调用
            System.Console.WriteLine($"{System.DateTime.Now} $ext_safeprojectname$DbContextFactory.CreateDbContext");
            System.AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
            var configuration = BuildConfiguration();

            var builder = new DbContextOptionsBuilder<$ext_safeprojectname$DbContext>()
                .UseNpgsql(configuration.GetConnectionString("MainConnectionString"));
            //    .ReplaceService<IMigrationsSqlGenerator, MyMigrationsSqlGenerator>();

            return new $ext_safeprojectname$DbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }
}
